package com.example.meitu.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.meitu.api.common.UserLoginContextHolder;
import com.example.meitu.api.domain.Media;
import com.example.meitu.api.domain.UnionMedia;
import com.example.meitu.api.domain.UnionMediaSub;
import com.example.meitu.api.dto.MediaInfoForUnion;
import com.example.meitu.api.dto.UnionMediaInfo;
import com.example.meitu.api.dto.UnionMediaInfo2;
import com.example.meitu.api.service.MediaService;
import com.example.meitu.api.service.UnionMediaService;
import com.example.meitu.api.dao.UnionMediaMapper;
import com.example.meitu.api.service.UnionMediaSubService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 蔡超
* @description 针对表【mt_union_media(媒体集群表)】的数据库操作Service实现
* @createDate 2024-06-27 15:26:06
*/
@Service
public class UnionMediaServiceImpl extends ServiceImpl<UnionMediaMapper, UnionMedia>
    implements UnionMediaService{

    @Resource
    private MediaService mediaService;

    @Resource
    private UnionMediaSubService unionMediaSubService;

    @Override
    public UnionMediaInfo unionInfo(Integer mediaId) {
        Media media = mediaService.getById(mediaId);
        if (media == null) {
            throw new RuntimeException("媒体ID不存在，mediaId：" + mediaId);
        }
        Integer unionId = media.getUnionMediaId();
        if (unionId == null) {
            throw new RuntimeException("媒体ID不存归一分类，mediaId：" + mediaId);
        }
        UnionMedia unionMedia = getById(unionId);
        UnionMediaInfo unionMediaInfo = new UnionMediaInfo();
        BeanUtils.copyProperties(unionMedia, unionMediaInfo);

        //查询分类下有权限的媒体
        List<Media> mediaList = mediaService.getMediaListByUnionId(unionId);
        List<MediaInfoForUnion> mediaInfoForUnionList = mediaList.stream()
                        .map(mediaInfo -> {
                            MediaInfoForUnion mediaInfoForUnion = new MediaInfoForUnion();
                            mediaInfoForUnion.setMediaId(mediaInfo.getId());
                            mediaInfoForUnion.setMediaName(mediaInfo.getMediaName());
                            mediaInfoForUnion.setBeforeName(mediaInfo.getBeforeName());
                            mediaInfoForUnion.setAnotherName(mediaInfo.getAnotherName());
                            mediaInfoForUnion.setIncludeName(mediaInfo.getIncludeName());
                            return mediaInfoForUnion;
                        }).collect(Collectors.toList());
        unionMediaInfo.setMediaList(mediaInfoForUnionList);
        return unionMediaInfo;
    }


    @Override
    public UnionMediaInfo2 unionInfo2(Integer mediaId, Integer subUnionId, String accountName) {
        UnionMediaSub unionMediaSub = unionMediaSubService.getById(subUnionId);
        UnionMedia unionMedia = getById(unionMediaSub.getUnionMediaId());
        UnionMediaInfo2 unionMediaInfo2 = new UnionMediaInfo2();
        BeanUtils.copyProperties(unionMedia, unionMediaInfo2);
        unionMediaInfo2.setUnionMediaId(unionMedia.getId());

        LambdaQueryWrapper<UnionMediaSub> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(UnionMediaSub::getUnionMediaId, unionMediaSub.getUnionMediaId());
        lambdaQueryWrapper.eq(UnionMediaSub::getDelFlag, '0');
        List<UnionMediaSub> unionMediaSubList = unionMediaSubService.list(lambdaQueryWrapper);
        // unionMediaSubList 要根据 subUnionId 设置 check
        Integer finalSubUnionId = subUnionId;
        unionMediaSubList = unionMediaSubList.stream().map(unionMediaSub1 -> {
            unionMediaSub1.setCheck(unionMediaSub1.getSubUnionId().equals(finalSubUnionId));
            return unionMediaSub1;
        }).collect(Collectors.toList());

        unionMediaInfo2.setSubUnionMediaList(unionMediaSubList);
        return unionMediaInfo2;
    }


}




